﻿using System;

namespace LayeredArchitectureSample.DomainModel
{
    [GenerateDataTransferObject(TemplateName = "DefaultDtoTemplate.tt")]
    public class WareUnitsCounted
    {
        private readonly Ware _ware;
        private readonly int _quantity;

        /// <summary>
        /// 
        /// </summary>
        /// <exception cref="ArgumentException"></exception>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        public WareUnitsCounted(Ware ware, int quantity)
        {
            if (ware == null) throw new ArgumentNullException("ware");

            if (quantity <= 0)
            {
                throw new ArgumentException("Quantity must be positive", "quantity");
            }

            _ware = ware;
            _quantity = quantity;
        }

        public int Quantity
        {
            get { return _quantity; }
        }

        public Ware Ware
        {
            get { return _ware; }
        }

        public bool Equals(WareUnitsCounted other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return Equals(other._ware, _ware) && other._quantity == _quantity;
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof (WareUnitsCounted)) return false;
            return Equals((WareUnitsCounted) obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                return (_ware.GetHashCode()*397) ^ _quantity;
            }
        }
    }
}